package dk.dashboardFP.dfpLogger.view {
	import dk.dashboardFP.dfpLogger.DFPLogItem;
	import dk.dashboardFP.dfpLogger.DFPLogger;
	import dk.dashboardFP.panels.ui.text.DFPLabel;
	import dk.dashboardFP.shapes.Box;

	import flash.display.Sprite;

	/**
	 * @author josef
	 */
	public class LogItemView extends Sprite {
		protected var _oDFPLogItem : DFPLogItem;
		protected var _doMessageLabel : DFPLabel;
		private var _doBack : LogItemBack;
		private var _nWidth : Number;
		private var _nSideMargin:Number = 2;
		private var _nItemWidth : Number;
		private var _nHeight : Number = -1;
		private var _nLabelOffset : Number = 8;
		private var _nColorBlockWidth : Number=6;
		private var _doColorBlock : Box;
		private var _nVerticslMargin : Number=2;

		
		public function LogItemView(oDFPLogItem:DFPLogItem) {
			_oDFPLogItem = oDFPLogItem;
			
			setupElements();
		}
		
		protected function setupElements() : void {
			
			//:::::::
			// message 
			_doMessageLabel=new DFPLabel(_oDFPLogItem.sMessage, false);
			addChild(_doMessageLabel);
			
			_doMessageLabel.makeMultiline();
			
			_doMessageLabel.x = _nSideMargin+_nLabelOffset;
			_doMessageLabel.y=_nVerticslMargin;
			
			_doMessageLabel.tfLabel.selectable=true;
			_doMessageLabel.tfLabel.border=false;
			
			// mouse settings 
			mouseEnabled=false;
			_doMessageLabel.mouseEnabled=false;
			_doMessageLabel.mouseChildren=true;
			
			//:::::::
			// log item back 
			_doBack=new LogItemBack(_oDFPLogItem.intLevel);
			addChildAt(_doBack, 0);
			
			_doBack.x = _nSideMargin;
			
			//:::::::
			// color block 
			_doColorBlock=new Box(_nColorBlockWidth, 10, DFPLogger.getLogGroupById(_oDFPLogItem.intGroup).color);
			addChild(_doColorBlock);
			
			_doColorBlock.x = _nSideMargin;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// size
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override public function set width(value : Number) : void {
			if (_nWidth != value) {
				_nWidth = value;
				updateWidth();
			}
		}

		override public function get width() : Number {
			return _nWidth;
		}

		override public function get height() : Number {
			return _nHeight;
		}

		private function updateWidth() : void {
			
			_nItemWidth = _nWidth - 2 * _nSideMargin;
			
			_doBack.width = _nItemWidth;
			_doMessageLabel.tfLabel.width = _nItemWidth - _nLabelOffset;
			
			if (_nHeight!=get_nHeight()) {
				updateHeight();
				dispatchEvent(new LogItemViewEvent(LogItemViewEvent.HEIGHT_UPDATED, true));
			}
		}
		
		protected function get_nHeight() : Number {
			return _doMessageLabel.tfLabel.height + _nVerticslMargin;
		}

		protected function updateHeight() : void {
			_nHeight=get_nHeight();
			_doBack.height = _nHeight;
			_doColorBlock.height = _nHeight;
			
			dispatchEvent(new LogItemViewEvent(LogItemViewEvent.HEIGHT_UPDATED, true));
		}
		
		public function get oDFPLogItem() : DFPLogItem {
			return _oDFPLogItem;
		}
	}
}
